package com.woniuxy.controller;

import com.woniuxy.pojo.Communication;
import com.woniuxy.service.CommunicationService;
import com.woniuxy.service.MyLog;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class CommunicationController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CommunicationService communicationService;
    //活动记录列表
    @GetMapping(value = "communications/selectLinkman")
    public DataGridView selectCommunication(String commCustNo){
        return communicationService.selectCommunicationByCommCustNo(commCustNo);
    }
    //增加活动记录
    @MyLog(value = "增加活动记录")  //这里添加了AOP的自定义注解
    @PostMapping(value = "/communications.do")
    public ResultDto addCommunication(Communication communication) {
        communication.setCommNo("co"+ new Date().getTime());
        Communication communicationReasult = this.communicationService.selectCommunicationBylinkNo(communication.getCommNo());
        if(communicationReasult!=null) {
            return ResultDto.USER_EXITS;
        }else {
            boolean flag=this.communicationService.addCommunication(communication);
            if (flag) {
                return ResultDto.USER_ADD_SUCCESS;
            } else {
                return ResultDto.USER_ADD_FAILURE;
            }
        }
    }
    @MyLog(value = "删除客户活动记录")  //这里添加了AOP的自定义注解
    //删除客户活动记录
    @DeleteMapping(value = "/communications/{commNo}")
    public ResultDto deleteCommunication(@PathVariable("commNo") String commNo){
        try {
            this.communicationService.deleteCommunicationBycommNo(commNo);
            return ResultDto.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_FAILURE;
        }
    }
    @MyLog(value = "批量删除顾客活动记录")  //这里添加了AOP的自定义注解
    //批量删除顾客活动记录
    @GetMapping(value = "communications/deleteBatch")
    public ResultDto deleteBathLinkman(@RequestParam("commNos[]") String[] commNos){
        try {
            this.communicationService.removeBatchCommunication(commNos);
            return ResultDto.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_FAILURE;
        }
    }
    @MyLog(value = "更新顾客活动记录")  //这里添加了AOP的自定义注解
    //更新顾客活动记录
    @PostMapping(value = "communications/update")
    public ResultDto updateCommunication(Communication communication){
        try {
            this.communicationService.updateCommunication(communication);
            return ResultDto.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.UPDATE_FAILURE;
        }
    }


}
